package cn.training.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.training.bean.UserBean;
import cn.training.service.HelloWorldService;


@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {

	@Autowired
	HelloWorldService helloWorldService;
	
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
        return "helloWorld";
    }
    
   // @RequestMapping(value = "/init", method = RequestMethod.POST,params="login")
   // public String initLogin(UserBean userBean, Model model) {
    	
    //	UserBean result = helloWorldService.searchUser(userBean);
    	//model.addAttribute("userBean", result);
      //  return "login";
    //}
    
    
    
    
    @RequestMapping(value = "/init1", method = RequestMethod.POST,params="login")
    public String initLogin(UserBean userBean, Model model) {
    	
    	List<UserBean> result = helloWorldService.searchUser(userBean);
    	model.addAttribute("List", result);
       // return "login";
    	 return "index";
    }
    
    
    
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="add")  
    public String intinsert(UserBean userBean, Model model) throws Exception  {
    	int insert = helloWorldService.insert(userBean);
    	if(insert==1)
    	{
    	    UserBean result= new UserBean();
    		model.addAttribute("userBean", result);
    		model.addAttribute("message", "ok");
    	     return "insertSuccess";
    	}else
    	{
    		throw new Exception();
    	}
    	    	
    }
  @RequestMapping(value = "/initupdate", method = RequestMethod.POST,params="update")  
  public String intupdate(UserBean userupdate, Model model)  { 
	  int update = helloWorldService.updateUser(userupdate); 
    	 
       if(update==1)
   	{
    	model.addAttribute("userBean", userupdate); 
   		model.addAttribute("message", "ok");
   	     return "login2";
  
   	}else
   	{
   		model.addAttribute("message", "false");
  	     return "login2";
   	}
  }
       @RequestMapping(value = "/deleteuser", method = RequestMethod.GET)
        public String initdelete(UserBean userdelete, Model model) {
        	
       	int result = helloWorldService.deleteUser(userdelete);
       	List<UserBean> res = helloWorldService.searchUser(userdelete);
    	model.addAttribute("List", res);
          return "index";
        }
        
       
   
  }
    

